package com.atguigu.rsa;

import com.sun.org.apache.xerces.internal.impl.dv.util.Base64;

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.PrivateKey;
import java.security.PublicKey;

public class RSAdemo {
    public static void main(String[] args) throws Exception{
        String input = "nmsl";

        //创建加密算法字符串
        String algorithm = "RSA";
        //公钥生成器
        KeyPairGenerator keyGenerator = KeyPairGenerator.getInstance(algorithm);
        KeyPair keyPair = keyGenerator.generateKeyPair();
        PrivateKey privateKey = keyPair.getPrivate();
        PublicKey publicKey = keyPair.getPublic();
        Cipher cipher = Cipher.getInstance(algorithm);
        //私钥加密
        cipher.init(Cipher.ENCRYPT_MODE,privateKey);
        byte[] jiami = cipher.doFinal(input.getBytes());
        System.out.println(new String(jiami));

        //私钥解密
        cipher.init(Cipher.DECRYPT_MODE,publicKey);
        byte[] jiemi = cipher.doFinal(jiami);
        System.out.println(new String(jiemi));


        System.out.println("sdadsadasd");



    }
}
